layui.use(['table', 'form', 'jquery', 'common'], function() {
    let table = layui.table;
	let form = layui.form;
	let $ = layui.jquery;
	let common = layui.common;
	let cols = [
			[{
					title: 'ID',
					field: 'id',
					align: 'center',
					width: 100
				},
				{
					title: '商户号',
					field: 'pid',
					align: 'center'
				},
				{
					title: '收件邮箱',
					field: 'mailname',
					align: 'center',
				},
				{
					title: '邮件标题',
					field: 'mailtitle',
					align: 'center',
				},
				{
					title: '发信时间',
					field: 'time',
					align: 'center'
				},
				{
					title: '发信状态',
					field: 'type',
					align: 'center',
					templet: function (d){
					    return d.type;
					}
				},
				{
					title: '操作',
					toolbar: '#options',
					align: 'center',
					width: 130
				}
			]
		]

		table.render({
			elem: '#user-table',
			url: '/h/ajax/qunemailgetDataList',
			page: true,
			cols: cols,
			cellMinWidth: 150,
			skin: 'line',
			toolbar: '#user-toolbar',
			defaultToolbar: [{
				title: '刷新',
				layEvent: 'refresh',
				icon: 'layui-icon-refresh',
			}, 'filter', 'print', 'exports']
		});
		table.on('tool(user-table)', function(obj) {
				if (obj.event === 'remove') {
					window.remove(obj);
				}else if(obj.event === 'allRemove'){
				   window.allRemove(); 
				}
		});
		table.on('toolbar(user-table)', function(obj) {
				if (obj.event === 'add') {
					window.add();
				}else if (obj.event === 'refresh') {
					window.refresh();
				}else if(obj.event === 'allRemove'){
				   window.allRemove(); 
				}
			});
        function screen() {
			if (typeof width !== 'number' || width === 0) {
				width = $(window).width() * 0.8;
			}
			if (typeof height !== 'number' || height === 0) {
				height = $(window).height() * 0.8;
			}
			return [width + 'px', height + 'px'];
		}
		// 添加数据
		window.add = function() {
			layer.open({
				type: 2,
				maxmin: true,
				title: '发送邮件',
				shade: 0.1,
				area: screen(),
				content: "./qunemail/add"
			});
		}
		form.on('select(userTypeSelect)', function(data) {
		    let userType = data.value;
		    if(userType === '2'){
		        $('#xf_email1').show();
		    }else{
		        $('#xf_email1').hide();
		    }
		});
		
        form.on('submit(addSave)', function(data) {
		    var ii = layer.msg('正在发信中！请耐心等待执行完毕...', {
                      icon: 6,
                      shade: 0.1,
                      time: 0
            });
			$.ajax({
				data: JSON.stringify(data.field),
				url: "/h/ajax/add_email",
				contentType: 'application/json',
				dataType: 'json',
				type: 'post',
				success: function(res) {
				    layer.close(ii);
					if (res.code == 200) {
						layer.msg(res.msg, {
							icon: 1,
							time: 2000
						}, function() {
							parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
							parent.layui.table.reload("user-table");
						});
					} else {
						layer.msg(res.msg, {
							icon: 2,
							time: 1000
						});
					}
				},
				error: function(data) {
    			     layer.close(ii);
    				 layer.msg("服务器错误！", {
							icon: 2,
							time: 1000
						});
    			}
			})
			return false;
		});
        window.allRemove = function(obj) {
			layer.confirm('确定要清理这些记录？', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
					url: "/h/ajax/emailallRemove",
					type: "POST",
					dataType: 'json',
					success: function(result) {
						layer.close(loading);
						if (result.code == 200) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								layui.table.reload("user-table");
							});
							
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
					error: function(data) {
        			     layer.close(loading);
        				 layer.msg("服务器错误！", {
								icon: 2,
								time: 1000
							});
        				return false;
        			}
				})
			})
		}
		window.remove = function(obj) {
			layer.confirm('确定删除这个记录？', {
				icon: 3,
				title: '提示'
			}, function(index) {
				layer.close(index);
				let loading = layer.load();
				$.ajax({
					url: "/h/ajax/emailRemove",
					data:{
					    id: obj.data['id'],
					},
					type: "POST",
					dataType: 'json',
					success: function(result) {
						layer.close(loading);
						if (result.code == 200) {
							layer.msg(result.msg, {
								icon: 1,
								time: 1000
							}, function() {
								layui.table.reload("user-table");
							});
							
						} else {
							layer.msg(result.msg, {
								icon: 2,
								time: 1000
							});
						}
					},
					error: function(data) {
        			     layer.close(loading);
        				 layer.msg("服务器错误！", {
								icon: 2,
								time: 1000
							});
        				return false;
        			}
				})
			})
		}
        window.refresh = function(param) {
			table.reload('user-table');
		}
    
    
})